<?php
class Model_pic extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	
	function save_title()
	{
		$insert_data = array(
		                       'title'=>$this->input->post('title'),
		                       'own' => '伟大的L',
		                       'created'=>time(),
		);
		$this->db->insert('pic_title',$insert_data);
		return $this->db->insert_id();
	}
	
	function save_pic($id)
	{
		$root = $_SERVER['DOCUMENT_ROOT'];
		$updir = "//upload//";
		$file = $_FILES['pic'];
		$des = $this->input->post('des');
                                    for($i=0;$i<count($file['name']);$i++)
		{
			$filename = $updir.$file['name'][$i];

		    $ret = move_uploaded_file($file['tmp_name'][$i],$root.$filename);
		    
                                        if(!ret)
                                        {
                                             echo "move_uploaded_file error!";
                                             exit();
                                        }
		    $insert_data = array(
		                          'id'=>$id,
		                          'image' => $filename,
                                                              'des'     =>$des[$i] ,
		                          'created'=>time(),		                                       
		    );
		    $this->db->insert('pic',$insert_data);
		}
	}
	
	function update_title()
	{
	   $id  = $this->input->post('id');
	   $id = intval($id);
	   $insert_data = array(
		                       'title'=>$this->input->post('title'),
							   //'des'=>$this->input->post('des'),
		                       'own' => '伟大的L',
		                       'created'=>time(),
		);
		$this->db->where('id',$id)->update('pic_title',$insert_data);
		return $id;
	}
	
	function update_pic($id)
	{
	   $root = $_SERVER['DOCUMENT_ROOT'];
		$updir = "//upload//";
		$file = $_FILES['pic'];
		$des = $this->input->post('des');
        for($i=0;$i<count($file['name']);$i++)
		{
			$filename = round(0,100).$updir.$file['name'][$i];

		    $ret = move_uploaded_file($file['tmp_name'][$i],$root.$filename);
		    
             if(!ret)
             {
                 echo "move_uploaded_file error!";
                 exit();
             }
		    $insert_data = array(
		                          'id'=>$id,
		                          'image' => $filename,
                                  'des'     =>$des[$i] ,
		                          'created'=>time(),		                                       
		    );
		    $this->db->insert('pic',$insert_data);
		}
	}
	
	function del_pic($id)
	{
		$this->db->where('id',$id)->delete('pic_title');
		$this->db->where('id',$id)->delete('pic');
	}
}